package com.walterweimod.auxiliary_attributes.event;

import com.google.common.collect.Lists;
import com.walterweimod.auxiliary_attributes.attribute.ModAttributes;
import com.walterweimod.auxiliary_attributes.capability.ModCapabilities;
import com.walterweimod.auxiliary_attributes.capability.impl.base.ChargeLivingEntityAttributeRangedValueBase;
import com.walterweimod.auxiliary_attributes.mobeffect.ModMobEffects;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.apache.commons.lang3.ObjectUtils;

import java.util.List;
import java.util.Objects;

import static com.walterweimod.auxiliary_attributes.Auxiliary_attributes.getLogger;
import static com.walterweimod.auxiliary_attributes.mobeffect.Rage.*;
import static com.walterweimod.auxiliary_attributes.mobeffect.Rage.getRageSpecialDefenseUUID;
import static com.walterweimod.auxiliary_attributes.util.ModAttributeUtils.addAttributeModifier;
import static com.walterweimod.auxiliary_attributes.util.ModAttributeUtils.removeAttributeModifier;
import static com.walterweimod.auxiliary_attributes.util.ModCapabilityUtils.*;
import static com.walterweimod.auxiliary_attributes.util.ModUtils.hyphenName;

@Mod.EventBusSubscriber
public class RageMainEvent {
    @SubscribeEvent(priority = EventPriority.HIGHEST)
    public static void beforeHurtUpdateAttribute(LivingHurtEvent event){
        LivingEntity livingEntity = event.getEntityLiving();
        MobEffectInstance rage = livingEntity.getEffect(ModMobEffects.RAGE.get());
        MobEffectInstance rage2 = livingEntity.getEffect(ModMobEffects.RAGE2.get());

        if (ObjectUtils.anyNotNull(rage, rage2)){
            List<MobEffectInstance> rages = Lists.newArrayList(rage, rage2);
            final int[] max_amplifier = {0};

            getLogger().info("同步战意值和属性值中，战意值为：{}", getCapabilityValue(
                    livingEntity,
                    ModCapabilities.RAGE_LAYER_VALUE,
                    ChargeLivingEntityAttributeRangedValueBase::getValue,
                    0
            ));

            rages.forEach(
                    instance -> {
                        if (Objects.nonNull(instance)
                                && instance.getAmplifier() > max_amplifier[0]){
                            max_amplifier[0] = instance.getAmplifier();
                        }
                    }
            );

            modifyAttributes(livingEntity, max_amplifier[0]);
        }
    }

    static void modifyAttributes(LivingEntity livingEntity, int amplifier){
        if (Objects.nonNull(livingEntity)){
            AttributeInstance defense = livingEntity.getAttribute(ModAttributes.DEFENSE.get());
            AttributeInstance attack = livingEntity.getAttribute(Attributes.ATTACK_DAMAGE);
            AttributeInstance special_defense = livingEntity.getAttribute(ModAttributes.SPECIAL_DEFENSE.get());

            int rage_layer = getCapabilityValue(
                    livingEntity,
                    ModCapabilities.RAGE_LAYER_VALUE,
                    ChargeLivingEntityAttributeRangedValueBase::getValue,
                    0
            );

            if (Objects.nonNull(defense)){
                removeAttributeModifier(
                        livingEntity,
                        defense.getAttribute(),
                        getRageDefenseUUID()
                );

                addAttributeModifier(
                        livingEntity,
                        defense.getAttribute(),
                        rage_layer * (0.05D + 0.03 * amplifier),
                        getRageDefenseUUID(),
                        AttributeModifier.Operation.MULTIPLY_BASE,
                        false
                );

                getLogger().info(hyphenName("Rage Defense", 15));
                getLogger().info("战意总防御力：{}", defense.getValue());
            }

            if (Objects.nonNull(attack)){
                removeAttributeModifier(
                        livingEntity,
                        attack.getAttribute(),
                        getRageAttackUUID()
                );

                addAttributeModifier(
                        livingEntity,
                        attack.getAttribute(),
                        rage_layer * (0.05D + 0.03 * amplifier),
                        getRageAttackUUID(),
                        AttributeModifier.Operation.MULTIPLY_BASE,
                        false
                );

                getLogger().info(hyphenName("Rage Attack", 15));
                getLogger().info("战意总攻击力：{}", attack.getValue());
            }

            if (Objects.nonNull(special_defense)){
                removeAttributeModifier(
                        livingEntity,
                        special_defense.getAttribute(),
                        getRageSpecialDefenseUUID()
                );

                addAttributeModifier(
                        livingEntity,
                        special_defense.getAttribute(),
                        rage_layer * (0.03D + 0.02 * amplifier),
                        getRageSpecialDefenseUUID(),
                        AttributeModifier.Operation.MULTIPLY_BASE,
                        false
                );

                getLogger().info(hyphenName("Rage Special Defense", 15));
                getLogger().info("战意总特殊防御力：{}", special_defense.getValue());
            }
        }
    }
}
